/*
 * MemberInstall.java 2011.01.01
 *
 * Copyright (c) 2010, MEI By Seok Kyun. Choi. (최석균)
 * http://syaku.tistory.com
 * 
 * GNU Lesser General Public License
 * http://www.gnu.org/licenses/lgpl.html
 */
package com.syaku.modules.member;

import java.util.*;

import org.apache.log4j.Logger;
import org.apache.commons.lang.*;
import org.apache.commons.configuration.*;

import com.syaku.core.util.*;
import com.syaku.modules.module.*;
import com.syaku.modules.menu.*;
import com.syaku.modules.install.*;

public class MemberInstall extends MemberObject {
  private Logger log = Logger.getLogger( this.getClass() );
  private ModuleObject daoModule = new ModuleObject();
  private MenuObject daoMenu = new MenuObject();
  private InstallObject daoInstall = new InstallObject();
  private PropertiesConfiguration CONFIG;

  private String module = modConfig.getString("mei.module.name");
  private String title = modConfig.getString("mei.module.title");
  private String rdate;
  private String config_name = "mei.install." + module;
  private String module_orl; // 싱글인 경우

  public void getInstallBefore() throws Exception {
    try {
      sqlMap.startTransaction();

      prepare();
      getTableCreate();

      sqlMap.commitTransaction();
    } catch (Exception e) {
      log.error(e.toString());
    } finally {
      sqlMap.endTransaction();
    }
  }

  public void getInstallAfter() throws Exception {
    try {
      sqlMap.startTransaction();

      prepare();
      getDataModuleInsert();
      getDataMenuInsert();
      getDataMemberGroupInsert();
      getDataMemberInsert();
      getPropModule();

      sqlMap.commitTransaction();
    } catch (Exception e) {
      log.error(e.toString());
    } finally {
      sqlMap.endTransaction();
    }
  }

  //install
  public void prepare() throws Exception {
    CONFIG = (PropertiesConfiguration) daoInstall.setModuleProperties();
    CONFIG.setAutoSave(true);
  }

 public void getTableCreate() throws Exception {
    sqlMemberCreate(null);
    sqlMemberAuthCreate(null);
    sqlMemberGroupCreate(null);
    sqlMemberGroupMemberCreate(null);

    rdate = DateUtils.date("yyyyMMddHHmmss");
    CONFIG.setProperty(config_name + ".rdate" , rdate);
    CONFIG.setProperty(config_name , true);
  }

  public void getPropModule() throws Exception {
    //생성된 모듈 데이터 프로퍼티 생성
    String get_module_orl = daoModule.getModule_orl(module);
    ModuleProperties prop = new ModuleProperties();
    prop.config2prop(get_module_orl);
  }

  public void getDataModuleInsert() throws Exception {
    String get_module_orl = daoModule.getModule_orl(module);
    this.module_orl = CONFIG.getString(config_name + ".module_orl" , get_module_orl );

    if ( StringUtils.isEmpty(module_orl) ) {
      ModuleBean modulebean = new ModuleBean();
      modulebean.setModule(module);
      modulebean.setMid(module);
      modulebean.setBrowser_title(title);
      modulebean.setRdate(rdate);
      module_orl = daoModule.moduleInsert(modulebean);
    }

    CONFIG.setProperty(config_name + ".module_orl",module_orl);
  }

  public void getDataMenuInsert() throws Exception {
    String menu_orl = "1";
    String parent_orl = "2";
    String menu_item_orl = CONFIG.getString(config_name + ".menu_item_orl");
    String url = "dispMemberAdminConfigInsert.me?mid=admin";

    if ( StringUtils.isEmpty(menu_item_orl) ) {
      menu_item_orl = daoMenu.getMenu_item_orl(title,url);

      if ( StringUtils.isEmpty(menu_item_orl) ) {
      MenuItemBean data = new MenuItemBean();
      data.setMenu_item_orl(null);
      data.setParent_orl(parent_orl);
      data.setNum( daoMenu.getMenuItemNumSort(menu_orl,parent_orl) );
      data.setMenu_orl(menu_orl);
      data.setUrl(url);
      data.setName(title);
      data.setOpen_window("0");
      data.setExpand("1");
      data.setIs_mobile("0");
      data.setRdate(rdate);
      menu_item_orl = daoMenu.sqlMenuItemInsert(data);
      }

      CONFIG.setProperty(config_name + ".menu_item_orl",menu_item_orl);
    }

  }

  public void getDataMemberGroupInsert() throws Exception {
    String group_orl_array[] = new String[3];
    String group_orl = null;

    if (sqlGroupCount(null) == 0) {
      CONFIG.clearProperty("mei.module.menu_item_orl");

      GroupBean groupbean = new GroupBean();
      groupbean.setGroup_orl(null);
      groupbean.setTitle("관리자");
      groupbean.setRegdate(rdate);
      groupbean.setIs_default("N");
      groupbean.setIs_admin("Y");
      group_orl = sqlGroupInsert(groupbean);
      group_orl_array[0] = group_orl;

      groupbean = new GroupBean();
      groupbean.setGroup_orl(null);
      groupbean.setTitle("회원");
      groupbean.setRegdate(rdate);
      groupbean.setIs_default("N");
      groupbean.setIs_admin("N");
      group_orl = sqlGroupInsert(groupbean);
      group_orl_array[1] = group_orl;

      groupbean = new GroupBean();
      groupbean.setGroup_orl(null);
      groupbean.setTitle("준회원");
      groupbean.setRegdate(rdate);
      groupbean.setIs_default("Y");
      groupbean.setIs_admin("N");
      group_orl = sqlGroupInsert(groupbean);
      group_orl_array[2] = group_orl;

      CONFIG.setProperty("mei.module.group_orl",group_orl_array);
    }
  }

  public void getDataMemberInsert() throws Exception {

    if ( isMemberUserIdOverlapCK("","admin") ) {
      MemberBean memberbean = new MemberBean();
      memberbean.setMember_orl(null);
      memberbean.setUser_id("admin");
      memberbean.setNickname("관리자");
      memberbean.setUser_name("관리자");
      memberbean.setPassword("1234");
      memberbean.setEmail("admin@mei.com");
      memberbean.setEmail_id("admin");
      memberbean.setEmail_host("mei.com");
      memberbean.setIs_admin("1");
      memberbean.setRdate(DateUtils.date("yyyyMMddHHmmss"));
      memberbean.setState("0");
      String member_orl = getMemberInsert(memberbean);
      CONFIG.setProperty("mei.module.member_orl",member_orl);
    }
  }

}